//
//  UpdateViewController.m
//  OurLottery
//
//  Created by hubing on 2018/9/3.
//  Copyright © 2018年 hl. All rights reserved.
//

#import "UpdateViewController.h"
#import "GCDWebUploader.h"
#import "SJXCSMIPHelper.h"
@interface UpdateViewController ()<GCDWebUploaderDelegate>
{
    GCDWebUploader * _webServer;
}
/* 显示ip地址 */
@property (nonatomic, weak) UILabel *showIpLabel;
/* fileTableView */
//@property (nonatomic, weak) UITableView *fileTableView;
/* fileArray */
@property (nonatomic, strong) NSMutableArray *fileArray;
@end

@implementation UpdateViewController

-(void)viewWillAppear:(BOOL)animated{
     [super viewWillAppear:animated];
     [self createUrlView];
    self.title = @"文件上传";
    self.view.backgroundColor = [UIColor whiteColor];
    // 文件存储位置
    NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSLog(@"文件存储位置 : %@", documentsPath);
    
    // 创建webServer，设置根目录
    _webServer = [[GCDWebUploader alloc] initWithUploadDirectory:documentsPath];
    // 设置代理
    _webServer.delegate = self;
    _webServer.allowHiddenItems = YES;
    
    // 限制文件上传类型
    _webServer.allowedFileExtensions = @[@"doc", @"docx", @"xls", @"xlsx", @"txt", @"pdf",@"mp3",@"jpeg",@"jpg",@"png",@"mp4"];
    // 设置网页标题
    _webServer.title = @"文件上传";
    // 设置展示在网页上的文字(开场白)
    _webServer.prologue = @"欢迎使用WIFI管理平台";
    // 设置展示在网页上的文字(收场白)
    _webServer.epilogue = @"可上传的文件格式包括：doc, docx, xls, xlsx, txt, pdf,jpeg,jpg,png,mp4";
    
    if ([_webServer start]) {
        self.showIpLabel.hidden = NO;
        self.showIpLabel.text = [NSString stringWithFormat:@"http://%@:%zd/", [SJXCSMIPHelper deviceIPAdress], _webServer.port];
    } else {
        self.showIpLabel.text = NSLocalizedString(@"GCDWebServer not running!", nil);
    }
   
    
    
    
    
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [_webServer stop];
    _webServer = nil;
    
}


-(void)createUrlView{
    UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"wifisend"]];
    [self.view addSubview:image];
    image.contentMode = UIViewContentModeScaleAspectFit;
    [image sizeWith:CGSizeMake(ScreenWidth, 100*Parameter)];
    [image layoutParentHorizontalCenter];
    [image alignParentTopWithMargin:20];
    
    UILabel *tips = [[UILabel alloc]init];
    tips.text = @"确保手机与电脑在同一局域网中";
    tips.textColor = [UIColor lightGrayColor];
    [self.view addSubview:tips];
    
    [tips sizeToFit];
    [tips layoutParentHorizontalCenter];
    [tips layoutBelow:image margin:30];
    
    UIImageView *tipImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tips"]];
    [self.view addSubview:tipImage];
    tipImage.contentMode = UIViewContentModeScaleAspectFit;
    [tipImage sizeWith:CGSizeMake(20, 20)];
    [tipImage alignRight:tips margin:CGRectGetWidth(tips.frame)+10];
    [tipImage layoutBelow:image margin:30];
    
    UILabel *title = [[UILabel alloc]init];
    title.text = @"传输方式";
    [self.view addSubview:title];
    title.font = [UIFont systemFontOfSize:20];
    
    [title sizeToFit];
    [title layoutParentHorizontalCenter] ;
    [title layoutBelow:tips margin:20];
    
    UILabel *subTitle = [[UILabel alloc]init];
    subTitle.text = @"在电脑浏览器中访问以下地址上传文件、视频";
    subTitle.textColor = [UIColor grayColor];
    [self.view addSubview:subTitle];
    
    [subTitle sizeToFit];
    [subTitle layoutParentHorizontalCenter];
    [subTitle layoutBelow:title margin:30];
    
    self.showIpLabel.backgroundColor = [UIColor lightGrayColor];
    self.showIpLabel.layer.cornerRadius = 5;
    self.showIpLabel.layer.masksToBounds = YES;
    [self.showIpLabel sizeWith:CGSizeMake(ScreenWidth - 80, 40)];
    [self.showIpLabel layoutParentHorizontalCenter];
    [self.showIpLabel layoutBelow:subTitle margin:40];
}




//#pragma mark - <UITableViewDataSource>
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//    return 1;
//}
//
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    return self.fileArray.count;
//}
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    static NSString *cellID = @"cell";
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
//    if (!cell) {
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
//    }
//    cell.textLabel.text = self.fileArray[indexPath.row];
//    return cell;
//}


//#pragma mark - <GCDWebUploaderDelegate>
//- (void)webUploader:(GCDWebUploader*)uploader didUploadFileAtPath:(NSString*)path {
//    NSLog(@"[UPLOAD] %@", path);
//
//    self.showIpLabel.hidden = YES;
//
//    NSString *string = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//    NSFileManager *fileManager = [NSFileManager defaultManager];
//    self.fileArray = [NSMutableArray arrayWithArray:[fileManager contentsOfDirectoryAtPath:string error:nil]];
//
//    [self.fileTableView reloadData];
//}
//
//- (void)webUploader:(GCDWebUploader*)uploader didMoveItemFromPath:(NSString*)fromPath toPath:(NSString*)toPath {
//    NSLog(@"[MOVE] %@ -> %@", fromPath, toPath);
//}
//
//- (void)webUploader:(GCDWebUploader*)uploader didDeleteItemAtPath:(NSString*)path {
//    NSLog(@"[DELETE] %@", path);
//
//    NSString *string = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//    NSFileManager *fileManager = [NSFileManager defaultManager];
//    self.fileArray = [NSMutableArray arrayWithArray:[fileManager contentsOfDirectoryAtPath:string error:nil]];
//
//    [self.fileTableView reloadData];
//}
//
//- (void)webUploader:(GCDWebUploader*)uploader didCreateDirectoryAtPath:(NSString*)path {
//    NSLog(@"[CREATE] %@", path);
//}

#pragma mark - <懒加载>
- (UILabel *)showIpLabel {
    if (!_showIpLabel) {
        UILabel *lb = [[UILabel alloc] init];
        
        lb.bounds = CGRectMake(0, 0, ScreenWidth, 200);
        lb.center = CGPointMake(ScreenWidth * 0.5, ScreenHeight * 0.5);
        lb.textColor = [UIColor darkGrayColor];
        lb.textAlignment = NSTextAlignmentCenter;
        lb.font = [UIFont systemFontOfSize:18.0];
        lb.numberOfLines = 0;
        
        [self.view addSubview:lb];
        _showIpLabel = lb;
    }
    return _showIpLabel;
}
//- (UITableView *)fileTableView {
//    if (!_fileTableView) {
//        UITableView *tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT) style:UITableViewStylePlain];
//
//        // 设置代理
//        tv.delegate = self;
//        // 设置数据源
//        tv.dataSource = self;
//        // 清除表格底部多余的cell
//        tv.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
//
//        [self.view addSubview:tv];
//        _fileTableView = tv;
//    }
//    return _fileTableView;
//}
- (NSMutableArray<NSString *> *)fileArray {
    if (!_fileArray) {
        _fileArray = [NSMutableArray array];
    }
    return _fileArray;
}


@end
